package com.base.controller.czb.czbadmin;

import com.base.po.acess.AcessToken;
import com.base.po.acess.TokenUtils;
import com.base.po.czb.GasOrderH5read;
import com.base.po.param.NetState;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.service.CZB.GasOrderH5readService;
import com.base.service.SubscribersService;
import com.base.service.UsersService;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("gas")
public class GasH5OrderAPi {
    @Autowired
    GasOrderH5readService gasOrderH5readService;
    @Autowired
    SubscribersService subscribersService;
    @Autowired
    UsersService usersService;

    @RequestMapping(value = "/getH5Order", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody
    NetState getOrder(String userId, Integer pageNo) {
        NetState netState = new NetState();

        if (userId == null) {
            netState.setErrorCode(NetState.PARAMS_ERROR);
            netState.setData("参数异常");
            return netState;
        }

        Page page = new Page();
        page.setCurrentPageNo(pageNo);
       Subscribers sub= subscribersService.selectByPrimaryKey(userId);
        List<GasOrderH5read> orders = null;
       int total= gasOrderH5readService.countAllGasOrder(sub.getParentid(),null,sub.getMobile(),null,null,null,null);
        page.setRecordTotal(total);
        orders = gasOrderH5readService.selectAllGasOrder(sub.getParentid(),null,sub.getMobile(),null,null,null,null,page);

        if (pageNo > page.getPageTotal()) {
            orders = null;
        }

        if (orders != null && orders.size() > 0) {
            netState.setJson(orders);
        }
        netState.setErrorCode(NetState.SUCCESS_CONN);
        netState.setData("查询成功");
        return netState;
    }

    @CrossOrigin
    @RequestMapping(value = "/getH5OrderToken", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody
    NetState getOrder1(HttpServletRequest request, Integer pageNo) {
        NetState net = new NetState();
        String AuthorizationToken = request.getHeader("SDB-Authorization");
        AcessToken acessToken = TokenUtils.getAcessToken(AuthorizationToken);
        // TOKEN获取失败
        if (acessToken == null || acessToken.equals("")) {
            net.setData("TOKEN不存在");
            net.setErrorCode(NetState.PARAMS_ERROR);
            return net;
        }
        // 获取代理商信息
        Users users = usersService.selectByPrimaryKey(acessToken.getParentid());
        if (users == null || users.getDatastatus() == false) {
            net.setData("代理商不存在");
            net.setErrorCode(NetState.PARAMS_ERROR);
            return net;
        }

        Subscribers sub = null; // 用户信息
        try {
            sub = subscribersService.selectBySubscribers(acessToken.getMobile(), users.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }

//        if (userId == null) {
//            net.setErrorCode(NetState.PARAMS_ERROR);
//            net.setData("参数异常");
//            return net;
//        }

        Page page = new Page();
        if (pageNo!=null){
            page.setCurrentPageNo(pageNo);
        }

        List<GasOrderH5read> orders = null;
        int total= gasOrderH5readService.countAllGasOrder(sub.getParentid(),null,sub.getMobile(),null,null,null,null);
        page.setRecordTotal(total);
        orders = gasOrderH5readService.selectAllGasOrder(sub.getParentid(),null,sub.getMobile(),null,null,null,null,page);

        if (pageNo > page.getPageTotal()) {
            orders = null;
        }

        if (orders != null && orders.size() > 0) {
            net.setJson(orders);
        }
        net.setErrorCode(NetState.SUCCESS_CONN);
        net.setData("查询成功");
        return net;
    }
}
